package com.lifeng.finance.demo.controller;

import com.lifeng.finance.demo.entity.Item;
import com.lifeng.finance.demo.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@ComponentScan(basePackages = {"com.lifeng.finance.demo.service.*"})
@RestController
public class ItemController {


    @Autowired
    private ItemService itemService;


    @RequestMapping(path = "/item/by_id")
    public Item findOneById(@RequestParam(name = "id") int id) {
        return itemService.findOneById(id);
    }

    @RequestMapping(path = "/item/by_project")
    public List<Item> findSomeByCustomerId(@RequestParam(name = "project_id") int projectId) {
        return itemService.findSomeByProjectId(projectId);
    }

    @PostMapping(path = "/item/add")

    public boolean add(@RequestBody Item item) {
        if (item.getName() == null || item.getDate() == null || item.getSpec() == null || item.getUnit() == null || item.getAmount() == 0.0 || item.getPrice() == 0.0) {
            return false;
        }
        try {
            itemService.add(item);
        } finally {

        }

        return true;
    }

    @PostMapping(path = "/item/update")
    public boolean update(@RequestBody Item item) {

        itemService.update(item);
        return true;
    }

    @RequestMapping(path = "item/delete")
    public boolean delete(@RequestParam(name = "id") int id) {
        itemService.delete(id);
        return true;
    }

}
